package com.dkm.service.impl;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dkm.entity.Department;
import com.dkm.repository.DepartmentRepository;
import com.dkm.repository.UsersRepository;
import com.dkm.service.DepartmentService;
import com.dkm.util.ResultUtil;

@Service
public class DepartmentServiceImpl implements DepartmentService {

	//注入部门持久层
	@Autowired
	private DepartmentRepository departmentRepository;
	
	//注入用户持久层
	@Autowired
	private UsersRepository usersRepository;
	
	@Override
	public Page<Department> findAllDepartments(Department department, Pageable pageable) {
		return departmentRepository.findAll(this.get(department), pageable);
	}

	private Specification<Department> get(Department department) {
		return new Specification<Department>() {

			@Override
			public Predicate toPredicate(Root<Department> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				//1、创建Predicate对象,动态sql表达式
				Predicate predicate=criteriaBuilder.conjunction();
				//2、创建一个集合，存放多个条件,动态sql表达式集合
				List<Expression<Boolean>> expressions=predicate.getExpressions();
				
				return predicate;
			}
		};
	}

	@Transactional //开启事务
	@Override
	public ResultUtil saveAndFlushDepartment(Department department) {
		ResultUtil resultUtil=null;
		String operation="";
		if(department.getDepartment_id()!=null) {
			operation="修改";
		}else {
			operation="添加";
			String uuid=UUID.randomUUID().toString();
			department.setDepartment_id(uuid);
		}
		
		
		Department d=departmentRepository.saveAndFlush(department);
		if(d!=null) {
			resultUtil=new ResultUtil(0, operation+"成功", d);
		}else {
			resultUtil=new ResultUtil(-1, operation+"失败", null);
		}
		return resultUtil;
	}

	@Transactional //开启事务
	@Override
	public ResultUtil deleteDepartment(String department_id) {
		//根据部门id获取用户数量
		Integer count=usersRepository.getUsersCountByDepartment_id(department_id);
		//如果有用户
		if(count>0) {
			return new ResultUtil(-1,"删除失败，该部门有["+count+"]位用户！", null);
		}
		
		Department department=new Department();
		department.setDepartment_id(department_id);
		try {
			departmentRepository.delete(department);
			return new ResultUtil(0, "删除成功", null);
		} catch (Exception e) {
			return new ResultUtil(-1,"删除失败", null);
		}
		
	}

	@Override
	public List<Department> getAllDepartments() {
		
		return departmentRepository.findAll();
	}

	@Override
	public Optional<Department> findById(String department_id) {

		return departmentRepository.findById(department_id);
	}

}
